use serde::Serialize;
use serde_json::{from_str};
use tokio::sync::OnceCell;
use tracing::info;
use crate::context::{get_file_or_create, read_to_string};

const PRODUCT_JSON_FILE_NAME: &str = "external/data/product.json";
static PRODUCTS: OnceCell<Vec<ProductCache>> = OnceCell::const_new();

#[derive(Debug, Clone, serde::Deserialize,Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProductCache {
    pub name: String,
    pub product_code: String,
    pub icon_class: String,
}
pub async fn get_products() -> &'static Vec<ProductCache> {
    PRODUCTS
        .get_or_init(|| async {
            info!("加载产品文件...");
            let mut file = get_file_or_create(PRODUCT_JSON_FILE_NAME).await.unwrap();
            let string = read_to_string(&mut file).await.unwrap();
            let value = from_str::<Vec<ProductCache>>(&string);
            if let Ok(value1) = value{
                value1
            }else{
                vec![]
            }
        })
        .await
}